
clear all
set more off
set gr off 
set maxvar 20000
 
cd /data2/cedsei/firm/

/* Inputs */ 

local in_ana    "/data2/cedsei/firm/C1_anaprep_v2_20230608.dta"

global c_controls "i.my i.sni3 i.r_s i.r_m_djl"
global b_controls "i.my i.sni3 i.r_s i.r_m_a"
global a_controls "age cumten s_age s_ten s"

/* Outputs */

********************************************************************************

clear 
use `in_ana'

quietly gen chi_perc=.
quietly gen chi_perc_djl=.

sort evyear chi
quietly egen temp=rank(chi_djl) if evyear==0
quietly gen temp2=temp-1 if evyear==0
quietly sum temp2 if evyear==0
scalar max=r(max)
quietly gen temp3=temp2/max
* line below is an ugly hack
quietly replace temp3=0.000000000001 if temp2==0
quietly replace chi_perc=.
quietly replace chi_perc_djl=.
forvalues x=1/100 {
 	display `x'
	local y = `x'/100
	local z = `x'/100 - 0.01
	quietly sum chi_djl if temp3 <=`y' & temp3 > `z'
	quietly replace chi_perc_djl=r(mean) if _n==`x'
	quietly sum chi if temp3 <=`y' & temp3 > `z'
	quietly replace chi_perc=r(mean) if _n==`x'
}
drop temp temp2 temp3

local outcome="chi_perc"
twoway ///
(qfit    `outcome' chi_perc_djl if _n<=100, lc(maroon))  ///
(lfit `outcome' chi_perc_djl if _n<=100, lc(gs9) lp(dash)) ///
(scatter `outcome' chi_perc_djl if _n<=100, m(Oh) mc(navy)), ytitle(Share of months on notice)  ///
xtitle(Instrument (Share of months on notice according to de jure + LIFO))  ylabel(0(0.2)0.8,grid) note("") legend(off) graphregion(color(white))
graphexportpdf temp_1, replace 

********************************************************************************

local co = 2 
foreach outcome in  ch_prod d_prod {
	 if "`outcome'"=="d_prod" {
	 	local ss = "Productivity rel. to prior 3-year average"
	 }
	 else if "`outcome'"=="ch_prod" {
	 	local ss = "Productivity rel. to t-1"
	}
	
	ivreg2 `outcome' (chi=chi_djl chi_djl2) m_djl $a_controls $c_controls if evyear==0, r first partial($c_controls)
	local b_chi=_b[chi]
	matrix v=e(V)
	local se_b=sqrt(v[1,1])
	quietly sum chi if evyear==0
	local a_`outcome'=round(1000*(1-exp(`b_chi'*r(mean)))/r(mean))/1000
	local se_`outcome'=round(1000*`se_b'/abs(`b_chi')*`a_`outcome'')/1000
	
	local b_chi = round(1000*abs(`b_chi'))
	local se_b = round(1000*`se_b')/1000

	reghdfe chi m_djl chi_djl chi_djl2 $a_controls if evyear==0, absorb($b_controls) vce(cluster lopnr_peorgnr)
	test chi_djl chi_djl2
	predict chi_p

	binscatter `outcome' chi_p if evyear==0, n(50) noa reportreg control(m_djl $a_controls $b_controls)  ///
	ytitle("`ss'") xtitle("Predicted share of months on notice (de jure + LIFO rules)") lc(navy*1.2) m(Oh) mc(gs8)  ///
	ylabel(-.1(0.05).1,grid) ///
	text(-0.015 -0.07 "IV-estimate: -0.`b_chi' (0`se_b')." "Estimate of alpha: 0`a_`outcome'' (0`se_`outcome'')", size(small))
	graphexportpdf temp_`co', replace 
	local co = `co'+1
	drop chi_p 
}


!gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=FirmAna_"$S_DATE".pdf temp*.pdf
!rm -f *temp*.pdf
!rm -f                 *.eps

